package org.lanlab.storode.controller;

import org.lanlab.storode.common.CommonResult;
import org.lanlab.storode.dal.entity.Project;
import org.lanlab.storode.service.ProjectService;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * @author jingchun
 * @since 2020/3/2
 */
@RestController
@RequestMapping("/projects")
public class ProjectController {

    private ProjectService projectService;

    public ProjectController(ProjectService projectService) {
        this.projectService = projectService;
    }

    @GetMapping
    public CommonResult<List<Project>> listAllProjects() {
        return CommonResult.success(projectService.listAllPages());
    }

    @GetMapping("/id/{id}")
    public CommonResult<Project> getProject(@PathVariable Long id) {
        Project project = projectService.getProjectById(id);
        return CommonResult.success(project);
    }

    @PostMapping
    public CommonResult<Boolean> addProject(@RequestBody Project project) {
        project = projectService.addProject(project);
        return Objects.isNull(project) ? CommonResult.failed() : CommonResult.success(true);
    }

    @PutMapping("/id/{id}")
    public CommonResult<Boolean> updateProject(@PathVariable Long id, @RequestBody Project project) {
        project.setId(id);
        project = projectService.updateProject(project);
        return Objects.isNull(project) ? CommonResult.failed() : CommonResult.success(true);
    }

}
